package com.xiaoyu.cloudmusic.service;

import com.xiaoyu.cloudmusic.dao.AdminMapper;
import com.xiaoyu.cloudmusic.pojo.ReportDetail;
import com.xiaoyu.cloudmusic.pojo.Song;
import com.xiaoyu.cloudmusic.pojo.StyleParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    AccountsService accountsService;
    @Autowired
    PlayListService playListService;

    @Override
    public int login(String name,String password) {
        return adminMapper.login(name,password);
    }

    @Override
    public int removeSongs(List<Integer> ids) {
        return adminMapper.removeSongs(ids);
    }

    @Override
    public int shelvesSongs(List<Integer> ids) {
        return adminMapper.shelvesSongs(ids);
    }

    @Override
    public boolean addSongs(List<Song> songList) {
        int count = 0;
        for(Song song:songList){
            adminMapper.addSong(song);
            count++;
        }
        return songList.size() == count;
    }

    @Override
    public boolean addReportInfo(ReportDetail reportDetail) {
        String reporter = accountsService.getUserInfoByUUID(reportDetail.getReporterUuid()).getNickname();
        String reported = accountsService.getUserInfoByUUID(reportDetail.getReportedUuid()).getNickname();
        reportDetail.setReporter(reporter);
        reportDetail.setReported(reported);
        int res = adminMapper.addReportInfo(reportDetail);
        return res == 1;
    }

    @Override
    public List<Song> paging(Integer pageNo, Integer pageSize) {
        return adminMapper.paging(pageNo,pageSize);
    }

    @Override
    public void SaveMusicStyleCount(HashMap<String, Integer> countMap) {
        Set<Map.Entry<String,Integer>> entries = countMap.entrySet();
        for(Map.Entry<String,Integer> entry:entries){
            adminMapper.updateStyleCount(entry.getKey(),entry.getValue());
        }
    }

    @Override
    public List<StyleParam> getStyleCounts() {
        return adminMapper.getStyleCounts();
    }

    @Override
    public void log_login(String uuid, String account, String time, String status) {
        adminMapper.log_login(uuid,account,time,status);
    }

    @Override
    public List<ReportDetail> getReportInfo() {
        return adminMapper.getReportInfo();
    }

    @Override
    public boolean delReportComment(Integer id, Integer commentid) {
        int res1 = playListService.delComment(commentid);
        int res2 = adminMapper.revokeReport(id);
        return res1 == res2;
    }

    @Override
    public int revokeReport(Integer id) {
        return  adminMapper.revokeReport(id);
    }

}
